package com.example.demo.controller;

import com.example.demo.common.HTTPResult;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.Articleinfo;
import com.example.demo.entity.Userinfo;
import com.example.demo.mapper.ArticleMapper;
import com.example.demo.service.ArticleService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
@RestController
@RequestMapping("/art")
public class ArtController {
    @Autowired
    private ArticleService articleService;

    // 根据用户 id 获取他的所有博客信息
    @RequestMapping("/mylist")
    public HTTPResult getMyList(HttpServletRequest request) {
        Userinfo userinfo = UserSessionUtils.getUserBySession(request);
        if(userinfo == null) {
            return HTTPResult.fail(-1, "非法请求");
        }
        List<Articleinfo> list = articleService.getMyList(userinfo.getId());
        return HTTPResult.success(list);
    }

    // 根据博客 id 逻辑删除博客信息
    @RequestMapping("/del")
    public HTTPResult del(Integer id, HttpServletRequest request) {
        if(id == null || id <= 0) {
            // 参数有误
            return HTTPResult.fail(-1, "参数异常");
        }
        Userinfo userinfo = UserSessionUtils.getUserBySession(request);
        if(userinfo == null) {
            return HTTPResult.fail(-2, "用户未登录");
        }
        Integer uid = userinfo.getId();
        Integer result = articleService.del(id, uid);
        return HTTPResult.success(result);
    }
}
